/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/
package com.zog 
{
	import com.pblabs.box2D.CollisionEvent;
	import com.pblabs.engine.core.ITickedObject;
	import com.pblabs.engine.core.InputMap;
	import com.pblabs.engine.core.ObjectTypeManager;
	import com.pblabs.engine.core.ProcessManager;
	import com.pblabs.engine.entity.EntityComponent;
	import com.pblabs.engine.entity.PropertyReference;
	import com.pblabs.engine.resource.*;
	import com.pblabs.engine.PBE;

	import flash.geom.Point;
	import flash.media.Sound;

	import com.pblabs.rendering2D.SpriteSheetRenderer;
	import com.pblabs.animation.AnimatorComponent;

	public class CharacterController extends EntityComponent implements ITickedObject 
    {
		
        [TypeHint(type="flash.geom.Point")]
		public var velocityReference:PropertyReference;
        
        public var isActive:Boolean = true;

		public function get input():InputMap {
			return _inputMap;
		}

		public function set input(value:InputMap):void {
			_inputMap = value;

			if (_inputMap!=null){
				_inputMap.mapActionToHandler("GoLeft", _OnLeft);
				_inputMap.mapActionToHandler("GoRight", _OnRight);
				_inputMap.mapActionToHandler("Jump", _OnJump);
			}
		}

		public function onTick(tickRate:Number):void {
            if(isActive)
            {
                var move:Number = _right-_left;
                var velocity:Point = owner.getProperty(velocityReference);
                velocity.x = move*100;

                if (_jump>0){
    //if (_sound != null)
    //   _sound.play();

                    velocity.y -= 200;
                    _jump = 0;
                }

                owner.setProperty(velocityReference, velocity);
            }
		}

		public function onInterpolateTick(factor:Number):void {
		}

		protected override function onAdd():void {
			PBE.processManager.addTickedObject(this);
			//PBE.resourceManager.load("../Assets/Sounds/testSound.mp3", MP3Resource, _OnSoundLoaded);

			owner.eventDispatcher.addEventListener(CollisionEvent.COLLISION_EVENT, _OnCollision);
			owner.eventDispatcher.addEventListener(CollisionEvent.COLLISION_STOPPED_EVENT, _OnCollisionEnd);
		}

		protected override function onRemove():void {
			_inputMap.destroy();// = null;
			owner.eventDispatcher.removeEventListener(CollisionEvent.COLLISION_EVENT, _OnCollision);
			owner.eventDispatcher.removeEventListener(CollisionEvent.COLLISION_STOPPED_EVENT, _OnCollisionEnd);

			PBE.resourceManager.unload("../Assets/Sounds/testSound.mp3", MP3Resource);
			PBE.processManager.removeTickedObject(this);
		}

		private function _OnCollision(event:CollisionEvent):void {
			if (PBE.objectTypeManager.doesTypeOverlap(event.collidee.collisionType, "Platform")){
				if (event.normal.y>0.7)
					_onGround++;
			}

			if (PBE.objectTypeManager.doesTypeOverlap(event.collider.collisionType, "Platform")){
				if (event.normal.y<-0.7)
					_onGround++;
			}
		}

		private function _OnCollisionEnd(event:CollisionEvent):void {
			if (PBE.objectTypeManager.doesTypeOverlap(event.collidee.collisionType, "Platform")){
				if (event.normal.y>0.7)
					_onGround--;
			}

			if (PBE.objectTypeManager.doesTypeOverlap(event.collider.collisionType, "Platform")){
				if (event.normal.y<-0.7)
					_onGround--;
			}
		}

		/*private function _OnSoundLoaded(resource:MP3Resource):void {
			_sound = resource.soundObject;
		}*/

		private function _OnLeft(value:Number):void 
        {
            _left = value;

            if(isActive)
            {
                var animator:AnimatorComponent = owner.lookupComponentByType(AnimatorComponent) as AnimatorComponent;

                if(_right == 0 && _left == 0)
                {
                    animator.play("idle");
                } 
                else if(_left == 0 && _right == 1) 
                {
                    runRight();
                }
                else
                {
                    runLeft();
                }
            }
        }
                
		private function _OnRight(value:Number):void 
        {
            _right = value;

            if(isActive)
            {
                var animator:AnimatorComponent = owner.lookupComponentByType(AnimatorComponent) as AnimatorComponent;

                if(_right == 0 && _left == 0)
                {
                    animator.play("idle");
                } 
                else if( _right == 0 && _left == 1) 
                {
                    runLeft();
                }
                else 
                {
                    runRight();
                }
            }
		}

        private function runLeft():void
        {
            var animator:AnimatorComponent = owner.lookupComponentByType(AnimatorComponent) as AnimatorComponent;
            animator.play("run");
            var spriteSheet:SpriteSheetRenderer = owner.lookupComponentByType(SpriteSheetRenderer) as SpriteSheetRenderer;
            //spriteSheet.size = new Point(-64, 64);
            spriteSheet.scale = new Point(-Math.abs(spriteSheet.scale.x), spriteSheet.scale.y);
        }
        
        private function runRight():void
        {
            var animator:AnimatorComponent = owner.lookupComponentByType(AnimatorComponent) as AnimatorComponent;
            animator.play("run");
            var spriteSheet:SpriteSheetRenderer = owner.lookupComponentByType(SpriteSheetRenderer) as SpriteSheetRenderer;
            //spriteSheet.size = new Point(64, 64);
            spriteSheet.scale = new Point(Math.abs(spriteSheet.scale.x), spriteSheet.scale.y);
        }

		private function _OnJump(value:Number):void 
        {
			if (_onGround>0)
				_jump = value;
		}

		private var _sound:Sound = null;
		private var _inputMap:InputMap;
		private var _left:Number = 0;
		private var _right:Number = 0;
		private var _jump:Number = 0;
		private var _onGround:int = 0;
	}
}